package main.java.com.library.controller;

import com.library.bean.User;
import com.library.service.UserService;
import com.library.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("utf-8");
       UserService us = new UserServiceImpl();
       String username = req.getParameter("username");
       String password = req.getParameter("password");
        Map<String, Object> map = us.userLogin(username, password);
        if(map.get("success")!=null){
            User user = (User)map.get("success");
            req.getSession().setAttribute("loginUser",user);
            req.getRequestDispatcher("success.jsp").forward(req,resp);
        }else{
            String msg = (String)map.get("fail");
            req.setAttribute("msg",msg);
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }


    }
}
